package com.lantosec.hmrp.modules.eval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.eval.entity.EvalScheme;
import com.lantosec.hmrp.modules.eval.entity.EvalSchemeDetailed;
import com.lantosec.hmrp.modules.eval.entity.EvalSchemeList;
import com.lantosec.hmrp.modules.eval.mapper.EvalSchemeMapper;
import com.lantosec.hmrp.modules.eval.service.IEvalSchemeService;
import com.lantosec.hmrp.modules.eval.vo.EvalItemVO;
import com.lantosec.hmrp.modules.eval.vo.EvalSchemeVO;
import com.lantosec.hmrp.modules.system.entity.SysDept;
import com.lantosec.hmrp.modules.system.entity.SysPosition;
import com.lantosec.hmrp.modules.system.entity.SysUser;
import com.lantosec.hmrp.modules.system.service.ISysDeptService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 考评方案 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-16
 */
@Service
@Primary
@Log4j2
@Transactional(rollbackFor = Exception.class)
public class EvalSchemeServiceImpl extends ServiceImpl<EvalSchemeMapper, EvalScheme> implements IEvalSchemeService {

    @Autowired
    private ISysDeptService iSysDeptService;

    @Override
    public List<EvalItemVO> getEvalSchemeParm(Integer evalSchemeId) {
        List<EvalItemVO> evalItemVO = baseMapper.getEvalSchemeParm(evalSchemeId);
        return evalItemVO;
    }

    @Override
    public Integer saveEvalScheme(EvalSchemeList evalSchemeList) {
        EvalScheme evalScheme = evalSchemeList.getEvalScheme();
        //新增考评方案
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String userName = TokenUtil.getName();
        evalScheme.setCreateUserId(userId);
        evalScheme.setCreateUser(userName);
        //当OwnerUserId为true时拥有者id为当前用户id,否则为null
        if(StringUtils.isNotEmpty(evalScheme.getOwnerUserId())){
            if(evalScheme.getOwnerUserId().equals("true")){
                evalScheme.setOwnerUserId(userId);
            }else {
                evalScheme.setOwnerUserId(null);
            }
        }else {
            evalScheme.setOwnerUserId(null);
        }
        evalScheme.setCreateTime(new Date());
        this.save(evalScheme);
        //新增考评方案明细
        List<EvalSchemeDetailed> evalSchemeDetailedList = evalSchemeList.getEvalSchemeDetailedList();
        if(null != evalSchemeDetailedList && !evalSchemeDetailedList.isEmpty()){
            evalSchemeDetailedList.forEach(parm->{
                parm.setEvalSchemeId(evalScheme.getEvalSchemeId());
                baseMapper.saveEvalSchemeDetailed(parm);
            });
        }
        return evalScheme.getEvalSchemeId();
    }

    @Override
    public void updateEvalScheme(EvalSchemeList evalSchemeList) {
        EvalScheme entity = evalSchemeList.getEvalScheme();
        //最后编辑人ID
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String userName = TokenUtil.getName();
        entity.setEditorUserId(userId);
        //最后编辑人
        entity.setEditorUser(userName);
        //最后编辑时间
        entity.setEditorTime(DateUtils.nowDate());
        //当OwnerUserId为true时拥有者id为当前用户id,否则为null
        if(StringUtils.isNotEmpty(entity.getOwnerUserId())){
            if(entity.getOwnerUserId().equals("true")){
                entity.setOwnerUserId(userId);
            }else {
                entity.setOwnerUserId(null);
            }
        }else{
            entity.setOwnerUserId(null);
        }

        //修改考评方案基本信息
        this.updateById(entity);
        //修改考评方案明细记录
        List<EvalSchemeDetailed> evalSchemeDetailedList = evalSchemeList.getEvalSchemeDetailedList();
        if(null != evalSchemeDetailedList && !evalSchemeDetailedList.isEmpty()){
            //先删除后新增
            baseMapper.deleteEvalSchemeDetailed(entity.getEvalSchemeId());
            evalSchemeDetailedList.forEach(parm->{
                parm.setEvalSchemeId(entity.getEvalSchemeId());
                baseMapper.saveEvalSchemeDetailed(parm);
            });
        }


    }

    @Override
    public List<TreeNodeVo> getDeptUserTree() {
        //查询所有的部门
        List<SysDept> deptList = iSysDeptService.list();
        List<TreeNodeVo> nodeVo = new ArrayList<>();
        deptList.forEach(parm->{
            String deptId = parm.getDeptId();
            List<TreeNodeVo> nodeVos = new ArrayList<>();
            //通过部门id查询用户
            List<SysUser> userList = baseMapper.selectDeptUser(deptId);
            if (null != userList && !userList.isEmpty()) {
                userList.forEach(parm1 -> {
                    TreeNodeVo treeNodeVo = new TreeNodeVo();
                    treeNodeVo.setKey(parm.getDeptId()+"-"+parm1.getUserId());
                    treeNodeVo.setTitle(parm1.getName()+"-"+parm.getDeptName());
                    nodeVos.add(treeNodeVo);
                });
            }
            nodeVo.add(new TreeNodeVo(parm.getDeptId(),parm.getDeptName(),nodeVos));
        });
        return nodeVo;
    }

    @Override
    public List<TreeNodeVo> deptUserTree() {
        //查询所有的部门
        List<SysDept> deptList = iSysDeptService.list();
        List<TreeNodeVo> nodeVo = new ArrayList<>();
        deptList.forEach(parm->{
            String deptId = parm.getDeptId();
            List<TreeNodeVo> nodeVos = new ArrayList<>();
            //通过部门id查询用户
            List<SysUser> userList = baseMapper.selectDeptUser(deptId);
            if (null != userList && !userList.isEmpty()) {
                userList.forEach(parm1 -> {
                    TreeNodeVo treeNodeVo = new TreeNodeVo();
                    treeNodeVo.setKey(parm.getDeptId()+"-"+parm1.getUserId());
                    treeNodeVo.setTitle(parm1.getName());
                    nodeVos.add(treeNodeVo);
                });
            }
            nodeVo.add(new TreeNodeVo(parm.getDeptId(),parm.getDeptName(),nodeVos));
        });
        return nodeVo;
    }

    @Override
    public List<TreeNodeVo> getDeptPositionTree() {
        //查询所有的部门
        List<SysDept> deptList = iSysDeptService.list();
        List<TreeNodeVo> nodeVo = new ArrayList<>();
        deptList.forEach(parm->{
            String deptId = parm.getDeptId();
            List<TreeNodeVo> nodeVos = new ArrayList<>();
            //通过部门id查询岗位
            List<SysPosition> positionList = baseMapper.selectDeptPosition(deptId);
            if (null != positionList && !positionList.isEmpty()) {
                positionList.forEach(parm1->{
                    TreeNodeVo treeNodeVo = new TreeNodeVo();
                    treeNodeVo.setKey(parm1.getPositionId());
                    treeNodeVo.setTitle(parm1.getPositionName());
                    nodeVos.add(treeNodeVo);
                });
            }
            nodeVo.add(new TreeNodeVo(parm.getDeptId(),parm.getDeptName(),nodeVos));
        });
        return nodeVo;
    }

    @Override
    public EvalSchemeList getEvalSchemeAll(Integer evalSchemeId) {
        EvalSchemeList evalSchemeList = new EvalSchemeList();
        //查询考评方案基本信息
        EvalScheme evalScheme = this.getById(evalSchemeId);
        //查询考评方案明细
        List<EvalSchemeDetailed> evalSchemeDetailedList = baseMapper.getEvalSchemeDetailed(evalSchemeId);
        evalSchemeList.setEvalScheme(evalScheme);
        evalSchemeList.setEvalSchemeDetailedList(evalSchemeDetailedList);
        return evalSchemeList;
    }

    @Override
    public List<EvalSchemeVO> getEvalScheme() {
        List<EvalScheme> evalSchemeList = this.list(new QueryWrapper<EvalScheme>());
        List<EvalSchemeVO> evalSchemeListVO = new ArrayList<>();
        evalSchemeList.forEach(parm->{
            EvalSchemeVO parmVO = parm.convertToVO();
            List<EvalSchemeDetailed> evalSchemeDetailedList = baseMapper.getEvalSchemeDetailed(parmVO.getEvalSchemeId());
            parmVO.setEvalSchemeDetailedList(evalSchemeDetailedList);
            evalSchemeListVO.add(parmVO);
        });
        return evalSchemeListVO;
    }

    @Override
    public PageVO<EvalSchemeVO> pageAll(PageVO<EvalScheme> page) {
        //查询拥有者有当前用户的考评方案（私有方案）,或者查询公共方案
        //获取当前用户id
        String userId = TokenUtil.getUserId();
        PageVO<EvalScheme> pageVO = this.page(page,new QueryWrapper<EvalScheme>().eq(EvalScheme.ColName.owner_user_id.toString(),userId)
                                                                                .or().isNull(EvalScheme.ColName.owner_user_id.toString())
                                                                                .orderByDesc(EvalScheme.ColName.create_time.toString()));
        PageVO<EvalSchemeVO> pageVO1 = new PageVO<>();
        List<EvalScheme> evalSchemeList = pageVO.getRecords();
        List<EvalSchemeVO> evalSchemeListVO = new ArrayList<>();
        evalSchemeList.forEach(parm->{
            EvalSchemeVO parmVO = parm.convertToVO();
            //查询方案下的考评项目
            List<EvalSchemeDetailed> evalSchemeDetailedList = baseMapper.getEvalSchemeDetailed(parmVO.getEvalSchemeId());
            parmVO.setEvalSchemeDetailedList(evalSchemeDetailedList);
            evalSchemeListVO.add(parmVO);
        });
        pageVO1.setRecords(evalSchemeListVO);
        pageVO1.setPageNo(pageVO.getPageNo());
        pageVO1.setTotal(pageVO.getTotal());
        pageVO1.setPageSize(pageVO.getPageSize());
        return pageVO1;
    }
}
